angular.module('baisonDaoGou', ['baisonDb', 'baisonService'])
        //导购模型----------------------------------------------------------
        .factory('BaseDaoGou', ['$q', 'dbSelect', '$rootScope', 'apiService', 'changeDate', function ($q, dbSelect, $rootScope, apiService, changeDate) {
                //获取商品大类信息  ======================================
                var get_category_list = function () {
//                  return apiService('base/category/get_list', {fields: '*'});
				  //改成从本地获取
				  var isShow="1";
				  var sql='select * from PosCategory where is_show="'+isShow+'"';
				   return dbSelect('YsPos',sql);
                };

                //获取商品小分类======================================
                var get_sort_list = function () {
//                  return apiService('base/sort/get_list', {fields: '*'});
				  //改成从本地获取
				   var shop_code = $rootScope.current_shop.ShopCode;
				   var sql='select s.* from PosSort s inner join Pos_shop_and_sort p on s.sort_code = p.sort_code where p.ShopCode = "'+shop_code+'"';
				   return dbSelect('YsPos',sql);

                };
                //获取商品的月销量
                var get_sales_by_day = function (sku, day) {
                    var date = changeDate(day, 1);
                    var sql = 'select sum(d.num) MonSales from PosReceiptDetail d left join PosReceiptRecord r on r.record_code = d.record_code where r.record_time >= "' + date + '" and d.sku="' + sku.SkuCode + '" and  r.is_return = 0 and r.is_checkout=1';
                    return dbSelect('YsPos', sql);
                };
                //获取商品的累计销量
                var get_allSales = function (sku) {
                    var sql = 'select sum(d.num) total_num from PosReceiptDetail d left join PosReceiptRecord r on r.record_code = d.record_code where d.sku="' + sku.SkuCode + '" and  r.is_return = 0 and r.is_checkout=1';
                    return dbSelect('YsPos', sql);
                };
                //获取商品大类信息从接口=================================
                var get_category_list_interface = function () {
                    return apiService('base/category/get_list', {fields: '*'});
                };
                //获取商品小类信息从接口=================================
                var get_sort_list_interface = function () {
                    return apiService('base/sort/get_list', {fields: '*'});

                };

                //获取所有带有热卖标记的商品
                var getHotGoods = function () {
                    var isHot = 1;
                    var sql = 'select s.*, g.is_hot from PosSku s inner join PosGoods g where s.GoodsCode = g.GoodsCode and g.is_hot = "' + isHot + '" group by g.GoodsCode limit 10';
                    return dbSelect('YsPos', sql);
                };
                //本地查找推荐商品
                var getRecommendGoods = function (GoodsCode) {
//                  var sql = 'select s.* from PosSku s inner join PosAssociatedGoods g on s.SkuCode = g.CollectionCode where g.SkuCode = "' + SkuCode + '"';
                    var sql = 'select s.* from PosSku s inner join PosAssociatedGoods g on s.GoodsCode = g.CollectionCode where g.GoodsCode = "'+GoodsCode+'" limit 1';
                    return dbSelect('YsPos', sql);
                };
                //获取导购商品评价
                var get_comments_by_goodsCode = function (goods_code) {
                    var param = {};
                    param.goods_code = goods_code;
                    return apiService('base/goods/get_byaucid', param);
                };

                //根据sku搜索商品
                var get_goods_by_code = function (code) {
                    var sql = 'select s.*,g.is_hot from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where s.SkuCode like "%' + code + '"';
                    return dbSelect('YsPos', sql);
                };

                //根据GoodsCode查出所有的sku(一个货号对应多个sku)
                var getSku_by_GoodCode = function (code) {
                    var sql = 'select * from PosSku where GoodsCode="' + code + '"';
                    return dbSelect('YsPos', sql);
                };
             
                //实时获取商品的库存
                var get_SKuNum_by_sku = function (sku) {
                    var sku_array = [];
                    sku_array.push({
                        'goods_code': sku.GoodsCode,
                        'color_code': sku.ColorCode,
                        'size_code': sku.SizeCode,
                        'sku': sku.SkuCode
                    });

                    var param = {};
                    param.store_code = $rootScope.current_shop.ShopCode;
                    param.skus = sku_array;
                    var sku_num = '';
                    return apiService('base/goods/get_goods_inv_by_skus', param);
                };

                //根据GoodsCode获取服务器的图片
                var get_pic_by_goodsCode = function (code) {
                    var param = {};
                    param.goods_code = code;
                    return apiService('pos/image/goods_list', param);
                };

                //根据sku查找该商品对应的促销
                var get_promotion_by_sku = function (sku) {

                    var deferredInstall = {};
                    var promiseInstall = {};
                    return dbSelect('YsPos', 'select pid from PosPromotionRuleGoods where sku="' + sku + '"').then(function (ret) {
                        $.each(ret, function (i, n) {
                            deferredInstall[i] = $q.defer();
                            promiseInstall[i] = deferredInstall[i].promise;
                            dbSelect('YsPos', 'select name,remark from PosPromotionRule where promotion_rule_id="' + n.pid + '"').then(function (ret) {
                                deferredInstall[i].resolve(ret[0]);
                            });
                        });
                        return $q.all(promiseInstall);
                    });
                };
                //统计大类商品条目数
				var get_num_by_cate = function(cate){	
					var sql = 'select count(*) num from PosGoods where CategoryCode="' + cate + '"';
                    return dbSelect('YsPos', sql);
				};
				//统计大类和品类的商品条目数
				var get_num_by_cate_and_sort = function(cate,sort){	
					var sql = 'select count(*) num from PosGoods where  CategoryCode="' + cate + '" and  SortCode = "'+sort+'"';
				//	console.log(sql);
                    return dbSelect('YsPos', sql);
				};
                return {
                    get_category_list: get_category_list,
                    get_sort_list: get_sort_list,
                    get_category_list_interface: get_category_list_interface,
                    get_sort_list_interface: get_sort_list_interface,
                    getSku_by_GoodCode: getSku_by_GoodCode,
                    get_goods_by_code: get_goods_by_code,
                    get_SKuNum_by_sku: get_SKuNum_by_sku,
                    get_pic_by_goodsCode: get_pic_by_goodsCode,
                    get_promotion_by_sku: get_promotion_by_sku,
                    getHotGoods: getHotGoods,
                    get_sales_by_day: get_sales_by_day,
                    get_allSales: get_allSales,
                    getRecommendGoods: getRecommendGoods,
                    get_comments_by_goodsCode: get_comments_by_goodsCode,
                    get_num_by_cate_and_sort:get_num_by_cate_and_sort,
                    get_num_by_cate:get_num_by_cate
                };
            }])
